/**
 ****************************************************************************************
 *
 * @file user_periph_setup.h
 *
 * @brief Peripherals setup header file. 
 *
 * Copyright (C) 2012. Dialog Semiconductor Ltd, unpublished work. This computer 
 * program includes Confidential, Proprietary Information and is a Trade Secret of 
 * Dialog Semiconductor Ltd.  All use, disclosure, and/or reproduction is prohibited 
 * unless authorized in writing. All Rights Reserved.
 *
 * <bluetooth.support@diasemi.com> and contributors.
 *
 ****************************************************************************************
 */

#ifndef _USER_PERIPH_SETUP_H_
    #define _USER_PERIPH_SETUP_H_



/*
 * INCLUDE FILES
 ****************************************************************************************
 */

    #include "rwip_config.h"
    #include "global_io.h"
    #include "arch.h"
    #include "da1458x_periph_setup.h"
    #include "i2c_eeprom.h"

/*
 * DEFINES
 ****************************************************************************************
 */

//*** <<< Use Configuration Wizard in Context Menu >>> ***

// <o> DK selection <0=> As in da1458x_periph_setup.h <1=> Basic <2=> Pro <3=> Expert
    #define HW_CONFIG (1)

    #define HW_CONFIG_BASIC_DK  ((HW_CONFIG==0 && SDK_CONFIG==1) || HW_CONFIG==1)
    #define HW_CONFIG_PRO_DK    ((HW_CONFIG==0 && SDK_CONFIG==2) || HW_CONFIG==2)
    #define HW_CONFIG_EXPERT_DK ((HW_CONFIG==0 && SDK_CONFIG==3) || HW_CONFIG==3)

//*** <<< end of configuration section >>>    ***


/****************************************************************************************/ 
/* i2c eeprom configuration                                                             */
/****************************************************************************************/

    #define I2C_EEPROM_SIZE   0x20000         // EEPROM size in bytes
    #define I2C_EEPROM_PAGE   256             // EEPROM's page size in bytes
    #define I2C_SPEED_MODE    I2C_FAST        // 1: standard mode (100 kbits/s), 2: fast mode (400 kbits/s)
    #define I2C_ADDRESS_MODE  I2C_7BIT_ADDR   // 0: 7-bit addressing, 1: 10-bit addressing
    #define I2C_ADDRESS_SIZE  I2C_2BYTES_ADDR // 0: 8-bit memory address, 1: 16-bit memory address, 3: 24-bit memory address


/****************************************************************************************/ 
/* SPI FLASH configuration                                                              */
/****************************************************************************************/

    #define SPI_FLASH_DEFAULT_SIZE  131072    // SPI Flash memory size in bytes
    #define SPI_FLASH_DEFAULT_PAGE  256
    #define SPI_SECTOR_SIZE         4096

    #ifndef __DA14583__
        #define SPI_EN_GPIO_PORT    GPIO_PORT_0
        #define SPI_EN_GPIO_PIN     GPIO_PIN_3

        #define SPI_CLK_GPIO_PORT   GPIO_PORT_0
        #define SPI_CLK_GPIO_PIN    GPIO_PIN_0

        #define SPI_DO_GPIO_PORT    GPIO_PORT_0
        #define SPI_DO_GPIO_PIN     GPIO_PIN_6

        #define SPI_DI_GPIO_PORT    GPIO_PORT_0
        #define SPI_DI_GPIO_PIN     GPIO_PIN_5
    #else // DA14583
        #define SPI_EN_GPIO_PORT    GPIO_PORT_2
        #define SPI_EN_GPIO_PIN     GPIO_PIN_3

        #define SPI_CLK_GPIO_PORT   GPIO_PORT_2
        #define SPI_CLK_GPIO_PIN    GPIO_PIN_0

        #define SPI_DO_GPIO_PORT    GPIO_PORT_2
        #define SPI_DO_GPIO_PIN     GPIO_PIN_9

        #define SPI_DI_GPIO_PORT    GPIO_PORT_2
        #define SPI_DI_GPIO_PIN     GPIO_PIN_4
    #endif


/* Enable WKUPCT. Required by wkupct_quadec driver. */
    #define WKUP_ENABLED


/****************************************************************************************/ 
/* LED and button configuration                                                               */
/****************************************************************************************/ 

    #if 1//HW_CONFIG_BASIC_DK //use this
		
        #define GPIO_ALERT_MOTOR_PORT     GPIO_PORT_0
        #define GPIO_ALERT_MOTOR_PIN      GPIO_PIN_4

				#define GPIO_DISMANTLE_CHECK1_PORT	 GPIO_PORT_2
				#define GPIO_DISMANTLE_CHECK1_PIN	 GPIO_PIN_2
				
				#define GPIO_DISMANTLE_CHECK2_PORT	 GPIO_PORT_0
				#define GPIO_DISMANTLE_CHECK2_PIN	 GPIO_PIN_2


        #define USE_PUSH_BUTTON         1
        #define GPIO_BUTTON_PORT        GPIO_PORT_2
        #define GPIO_BUTTON_PIN         GPIO_PIN_3

				#define GPIO_ADC_BAT_PORT       GPIO_PORT_0
				#define GPIO_ADC_BAT_PIN       GPIO_PIN_1

				#define GPIO_CHG_DET_PORT			GPIO_PORT_2
				#define GPIO_CHG_DET_PIN			GPIO_PIN_4

				#define GPIO_PWR_HOLD_PORT			GPIO_PORT_0
				#define GPIO_PWR_HOLD_PIN				GPIO_PIN_7

#if 0
        #define USE_BAT_LEVEL_ALERT     0
        #define GPIO_BAT_LED_PORT       GPIO_PORT_2
        #define GPIO_BAT_LED_PIN        GPIO_PIN_1
#endif

				#define USE_I2C 1
				#define I2C_PORT    GPIO_PORT_1
				#define I2C_SCL_PIN     GPIO_PIN_2
				#define I2C_SDA_PIN     GPIO_PIN_3

				#define USE_OLED_SH1107 1
				#if USE_OLED_SH1107
				#define OLED_RES_PORT GPIO_PORT_1
				#define OLED_RES_PIN GPIO_PIN_0
				#define OLED_VPP_EN_PORT GPIO_PORT_1
				#define OLED_VPP_EN_PIN GPIO_PIN_1
				
				#endif
				
				
    #elif HW_CONFIG_PRO_DK
        #define GPIO_ALERT_LED_PORT     GPIO_PORT_1
        #define GPIO_ALERT_LED_PIN      GPIO_PIN_0

        #define USE_PUSH_BUTTON         1
        #define GPIO_BUTTON_PORT        GPIO_PORT_1
        #define GPIO_BUTTON_PIN         GPIO_PIN_1

        #define USE_BAT_LEVEL_ALERT     0
        #define GPIO_BAT_LED_PORT       GPIO_PORT_1
        #define GPIO_BAT_LED_PIN        GPIO_PIN_2
    #else // HW_CONFIG_EXPERT_DK
        #define GPIO_ALERT_LED_PORT     GPIO_PORT_0
        #define GPIO_ALERT_LED_PIN      GPIO_PIN_7

        #define USE_PUSH_BUTTON         1
        #define GPIO_BUTTON_PORT        GPIO_PORT_1
        #define GPIO_BUTTON_PIN         GPIO_PIN_1

        #define USE_BAT_LEVEL_ALERT     1
        #define GPIO_BAT_LED_PORT       GPIO_PORT_1
        #define GPIO_BAT_LED_PIN        GPIO_PIN_0
    #endif

#ifdef CFG_PRINTF_UART2

// Select UART settings
#define UART2_BAUDRATE     UART_BAUDRATE_115K2       // Baudrate in bits/s: {9K6, 14K4, 19K2, 28K8, 38K4, 57K6, 115K2}
#define UART2_DATALENGTH   UART_CHARFORMAT_8         // Datalength in bits: {5, 6, 7, 8}
#define UART2_PARITY       UART_PARITY_NONE          // Parity: {UART_PARITY_NONE, UART_PARITY_EVEN, UART_PARITY_ODD}
#define UART2_STOPBITS     UART_STOPBITS_1           // Stop bits: {UART_STOPBITS_1, UART_STOPBITS_2}
#define UART2_FLOWCONTROL  UART_FLOWCONTROL_DISABLED // Flow control: {UART_FLOWCONTROL_DISABLED, UART_FLOWCONTROL_ENABLED}

#define UART2_GPIO_PORT    GPIO_PORT_2
#define UART2_TX_PIN       GPIO_PIN_8
#define UART2_RX_PIN       GPIO_PIN_9
#define UART_ENABLED

#endif
/*
 * FUNCTION DECLARATIONS
 ****************************************************************************************
 */
 
void periph_init(void);

void GPIO_reservations(void);

void alert_motor_set_on(bool on);
bool alert_motor_get_status(void);

void power_hold_set_on(bool on);
bool app_is_power_hold_on(void);

bool app_is_power_key_down(void);

bool app_is_battery_charging(void);

#endif //_USER_PERIPH_SETUP_H_
